#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
#include <arpa/inet.h>
int main(int argc, char* argv[]) 
{
    if (argc != 4) 
    {
        std::cerr << "Usage: client <server_ip> <server_port> <message>\n";
        return 1;
    }

    const char* server_ip = argv[1];
    int server_port = std::stoi(argv[2]);
    const char* message = argv[3];

    int client_socket;
    struct sockaddr_in server_addr;

    // 创建客户端套接字
    client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (client_socket < 0) 
    {
        std::cerr << "Error creating socket\n";
        return 1;
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(server_port);
    inet_pton(AF_INET, server_ip, &server_addr.sin_addr);

    // 连接到服务器
    if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) 
    {
        std::cerr << "Error connecting to server\n";
        close(client_socket);
        return 1;
    }

    // 发送消息
    send(client_socket, message, strlen(message), 0);

    // 关闭套接字
    close(client_socket);

    return 0;
}